#include <time.h>
#include "GPS_IPC.h"

//
// Created by ubuntu on 6/10/23.
//
int current = 0;
int gps_status[4] = {0, 1, 2, 6};

char *getRandomStr() {

}

void client() {
    int qid;
    message msg;
    if ((qid = msgget(SVKEY, 0666)) == -1) {
        perror("msgget");
        exit(1);
    }

    printf("Open queue %d\n", qid);
    while (1) {

        char result[512];
        char *str = "$GPGGA,%d%d%d.%ld,%04.4f,N,%05.4f,E,%d,03,4.4,32.6,M,5.4,M,%d%d%d.%ld,%04d*5A";
        time_t now = time(NULL);
        // 格式化并打印时分秒
        struct tm *timeinfo = localtime(&now);
        struct timeval tv;
        double latitude = rand() % 1000 / 10.0;
        double longitude = rand() % 1000 / 10.0;
        int differential_station_id = rand() % 1024;
        sprintf(result, str, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, tv.tv_usec % 1000, latitude, longitude,
                gps_status[current++ % 4], timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, tv.tv_usec % 1000,
                differential_station_id);
        printf("%s\n", result);

        strcpy(msg.msg_text, result);
        msg.msg_type = getpid();
        if (msgsnd(qid, &msg, strlen(msg.msg_text), 0) < 0) {
            perror("message posted!");
            exit(1);
        }
        if (strncmp(msg.msg_text, "quit", 4) == 0) {
            break;
        }
        sleep(1);
    }
    exit(0);

}

int main(int arg, char **args) {
    client();
    return 0;
}